﻿using AutoMapper;
using Modules.Corp;
using Modules.Person;
using Modules.Project;
using Modules.System.Attachment;
using PmSoft;
using PmSoft.ApplicationContext;
using System.ComponentModel.DataAnnotations;
using static Modules.Project.ProjectConstants;

namespace ManagementApi.Models.Project
{
    /// <summary>
    /// 项目管理员注册项目表单信息
    /// </summary>
    public class ProjectRegisterForm : ProjectCheckInForm
    {
        /// <summary>
        /// 总包企业Id
        /// </summary>
        [Display(Name = "总包企业Id")]
        [Required(ErrorMessage = "{0}不能为空")]
        public int LumpContractorCorpId { get; set; }

        /// <summary>
        /// 总包企业联系电话
        /// </summary>
        [Display(Name = "总包企业联系电话")]
        public string? LumpContractorCorpPhoneNum { get; set; }

        /// <summary>
        /// 监理企业id
        /// </summary>
        [Display(Name = "监理企业id")]
        public int? SupervisorCorpId { get; set; }

        /// <summary>
        /// 总包企业名称
        /// </summary>
        public string? LumpContractorCorpName { get; set; }

        /// <summary>
        /// 总包企业社会信用代码
        /// </summary>
        public string? LumpContractorCorpCode { get; set; }

        /// <summary>
        /// 总包合同金额
        /// </summary>
        [Display(Name = "总包合同金额")]
        [Required(ErrorMessage = "{0}不能为空")]
        public decimal LumpContractorCorpAmount { get; set; }

        /// <summary>
        /// 总包企业入场时间
        /// </summary>
        [Display(Name = "总包企业入场时间")]
        [Required(ErrorMessage = "{0}不能为空")]
        public DateTime LumpContractorCorpStartDate { get; set; }

        /// <summary>
        /// 总包企业离场时间
        /// </summary>
        [Display(Name = "总包企业离场时间")]
        [Required(ErrorMessage = "{0}不能为空")]
        public DateTime LumpContractorCorpEndDate { get; set; }

        /// <summary>
        /// 监理企业名称
        /// </summary>
        public string? SupervisorCorpName { get; set; }

        /// <summary>
        /// 监理企业社会信用代码
        /// </summary>
        public string? SupervisorCorpCode { get; set; }

        /// <summary>
        /// 监理企业合同金额
        /// </summary>
        [Display(Name = "监理企业合同金额")]
        public decimal? SupervisorCorpAmount { get; set; }

        /// <summary>
        /// 监理企业入场时间
        /// </summary>
        [Display(Name = "监理企业入场时间")]
        public DateTime? SupervisorCorpStartDate { get; set; }

        /// <summary>
        /// 监理企业离场时间
        /// </summary>
        [Display(Name = "监理企业离场时间")]
        public DateTime? SupervisorCorpEndDate { get; set; }

        /// <summary>
        /// 是否有监理单位
        /// </summary>
        [Display(Name = "是否有监理单位")]
        [Required(ErrorMessage = "{0}不能为空")]
        public bool HasSupervisor { get; set; }

        /// <summary>
        /// 中标通知书附件列表
        /// </summary>
        public List<AttachmentInfo>? AttachmentFiles { get; set; }

        /// <summary>
        /// 担保文件附件列表
        /// </summary>
        public List<AttachmentInfo>? EarnestAttachmentFiles { get; set; }

        /// <summary>
        /// 无监理证明附件列表
        /// </summary>
        public List<AttachmentInfo>? NoSupervisorAttachmentsFiles { get; set; }

        /// <summary>
        /// 多地施工证明附件列表
        /// </summary>
        public List<AttachmentInfo>? PositionAttachmentsFiles { get; set; }

        /// <summary>
        /// 扩展字段 审核失败原因
        /// </summary>
        public string? AuditReason { get; set; }

        public 项目审核状态? AuditStatus { get; set; }
    }

    public static class ProjectRegisterExtensions
    {
        public static ProjectRegisterForm AsForm(this ProjectInfo projectInfo, IApplicationContext applicationContext)
        {
            ProjectRegisterForm form = new ProjectRegisterForm();
            var mapper = applicationContext.GetService<IMapper>();
            mapper.Map(projectInfo, form);

            var corpService = ServiceAccessor.GetService<CorpService>();
            if (projectInfo.LumpContractorCorpId != null)
            {
                var lumpContractorCorp = corpService.GetCorpInfo((int)projectInfo.LumpContractorCorpId);
                if (lumpContractorCorp != null)
                {
                    form.LumpContractorCorpId = lumpContractorCorp.CorpId;
                    form.LumpContractorCorpName = lumpContractorCorp.Name;
                    form.LumpContractorCorpCode = lumpContractorCorp.Code;
                }
            }
            if (projectInfo.SupervisorCorpId != null)
            {
                var supervisorCorp = corpService.GetCorpInfo((int)projectInfo.SupervisorCorpId);
                if (supervisorCorp != null)
                {
                    form.SupervisorCorpId = supervisorCorp.CorpId;
                    form.SupervisorCorpName = supervisorCorp.Name;
                    form.SupervisorCorpCode = supervisorCorp.Code;
                }
            }
            var projectAttachmentService = applicationContext.GetService<ProjectAttachmentService>();
            if (form.ProjectId.HasValue)
            {
                form.AttachmentFiles = projectAttachmentService.GetSysAttachmentByObjectIdAndType((int)form.ProjectId, 附件类型.中标通知书, 项目附件租户.Project).ToList();
                form.EarnestAttachmentFiles = projectAttachmentService.GetSysAttachmentByObjectIdAndType((int)form.ProjectId, 附件类型.担保文件, 项目附件租户.Project).ToList();
                form.NoSupervisorAttachmentsFiles = projectAttachmentService.GetSysAttachmentByObjectIdAndType((int)form.ProjectId, 附件类型.无监理证明, 项目附件租户.Project).ToList();
                form.PositionAttachmentsFiles = projectAttachmentService.GetSysAttachmentByObjectIdAndType((int)form.ProjectId, 附件类型.多地施工证明, 项目附件租户.Project).ToList();
            }
            return form;
        }
    }
}